import request from '@/utils/request'
import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
import { getGoodsSelector } from '@/api/services/goods'
export default {
  components: {},
  props: [],
  data() {
    return {
      loading: false,
      visible: false,
      isDetail: false,
      dataForm: {
        id: '',
        enCode: undefined,
        fullName: undefined,
        shortName: undefined,
        openDate: undefined,
        telephone: undefined,
        email: undefined,
        manager: undefined,
        managerMobile: undefined,
        area: undefined,
        wechat: undefined,
        address: undefined,
        fullAddress: undefined,
        insurance: undefined,
        insuranceNo: undefined,
        insuranceDate: undefined,
        businessName: undefined,
        businessNo: undefined,
        siteArea: undefined,
        type: undefined,
        campusContractList: [],
        campusContactList: [],
        campusAccountList: [],
        endDate: undefined,
        startDate: undefined,
        createUserName: undefined,
        createTime: undefined,
        updateUserName: undefined,
        updateTime: undefined,
        description: undefined,
        personnel: undefined,
        schoolSituation: undefined,
        contract: undefined,
        cloudPlatform: undefined,
        franchise: undefined,
        renewal: undefined,
        deposit: undefined,
        coopMode: undefined,
        status: undefined
      },
      // 开始结束日期限制
      pickerOptionsStart2: {
        disabledDate: time => {
          if (this.dataForm.endTime) {
            return time.getTime() >= new Date(this.dataForm.endTime).getTime()
          }
        }
      },
      // 结束日期限制
      pickerOptionsEnd2: {
        disabledDate: time => {
          if (this.dataForm.startDate) {
            return time.getTime() <= new Date(this.dataForm.startDate).getTime()
          }
        }
      },
      rules: {
        enCode: [
          {
            required: true,
            message: '请输入校区编码',
            trigger: 'blur'
          }
        ],
        fullName: [
          {
            required: true,
            message: '请输入校区全称',
            trigger: 'blur'
          }
        ],
        type: [
          {
            required: true,
            message: '请选择校区类型',
            trigger: 'change'
          }
        ],
        shortName: [
          {
            required: true,
            message: '请输入校区简称',
            trigger: 'blur'
          }
        ],
        email: [
          {
            pattern: /^[a-z0-9]+([._\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/,
            message: '请输入正确的邮箱',
            trigger: 'blur'
          }
        ],
        manager: [
          {
            required: true,
            message: '请输入校区负责人',
            trigger: 'blur'
          }
        ],
        managerMobile: [
          {
            required: true,
            message: '请输入负责人电话',
            trigger: 'blur'
          }
        ],
        area: [
          {
            required: true,
            message: '请选择行政区域',
            trigger: 'change'
          }
        ],
        address: [
          {
            required: true,
            message: '请输入详细地址',
            trigger: 'blur'
          }
        ]
      },
      typeOptions: [],
      contractTypeOptions: [],
      payStatusOptions: [],
      genderOptions: [],
      // identityOptions: [],
      goodsOptions: [],
      sourceOptions: [{ fullName: '订单', enCode: '1' }, { fullName: '赠送', enCode: '2' }],
      statusOptions: [{ fullName: '正常', enCode: '1' }, { fullName: '退款', enCode: '2' }, { fullName: '作废', enCode: '4' }],
      active1: '0'
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getTypeOptions()
    this.getStatusOptions()
    this.getTypeOptions()
    this.getPayStatusOptions()
    this.getGenderOptions()
    // this.getIdentityOptions()
    this.getGoodsSelector()
    this.getContractTypeOptions()
  },
  mounted() {},
  methods: {
    getGoodsSelector() {
      getGoodsSelector().then(res => {
        this.goodsOptions = res.data
      })
    },
    getTypeOptions() {
      getDictionaryDataSelector('CampusType').then(res => {
        this.typeOptions = res.data.list
      })
    },
    getContractTypeOptions() {
      getDictionaryDataSelector('SignType').then(res => {
        this.contractTypeOptions = res.data.list
      })
    },
    getStatusOptions() {
      getDictionaryDataSelector('CampusStatus').then(res => {
        this.statusOptions = res.data.list
      })
    },
    getPayStatusOptions() {
      getDictionaryDataSelector('PayStatus').then(res => {
        this.payStatusOptions = res.data.list
      })
    },
    getGenderOptions() {
      getDictionaryDataSelector('sex').then(res => {
        this.genderOptions = res.data.list
      })
    },
    // getIdentityOptions() {
    //   getDictionaryDataSelector('CustomerIdentity').then(res => {
    //     this.identityOptions = res.data.list
    //   })
    // },
    goBack() {
      this.$emit('refresh')
    },
    init(id, isDetail) {
      this.dataForm.id = id || ''
      this.visible = true
      this.isDetail = isDetail || false
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields()
        if (this.dataForm.id) {
          request({
            url: 'api/campus/' + this.dataForm.id,
            method: 'get'
          }).then(res => {
            this.dataForm = res.data
            console.log(this.dataForm)
          })
        }
      })
    },
    dataFormSubmit() {
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          this.loading = true
          if (!this.dataForm.id) {
            request({
              url: `api/campus`,
              method: 'post',
              data: this.dataForm
            })
              .then(res => {
                this.$message({
                  message: res.msg,
                  type: 'success',
                  duration: 1000,
                  onClose: () => {
                    this.visible = false
                    this.loading = false
                    this.$emit('refresh', true)
                  }
                })
              })
              .catch(res => {
                console.log(res)
                this.loading = false
              })
          } else {
            request({
              url: 'api/campus/' + this.dataForm.id,
              method: 'PUT',
              data: this.dataForm
            })
              .then(res => {
                this.$message({
                  message: res.msg,
                  type: 'success',
                  duration: 1000,
                  onClose: () => {
                    this.visible = false
                    this.loading = false
                    this.$emit('refresh', true)
                  }
                })
              })
              .catch(res => {
                console.log(res)
                this.loading = false
              })
          }
        }
      })
    }
  }
}
